
<!DOCTYPE html >
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>

<p>计数: <output id="result"></output></p>
<button onClick="startWorker()">开始 Worker</button> 
<button onClick="stopWorker()">停止 Worker</button>
<br /><br />
<button onClick="locfor()">本页面循环</button> 
<button onClick="workerfor()">worker循环</button> 
<br /><br />
<button onClick="Promisefor()">Promise异步</button> 
<br /><br />
<img src="">

</body>
</html>


<script>

function workerfor ()
{
var cout=0;
console.log("start")
console.time("start")
	var ww=new Worker("for_workers.js");
	ww.onmessage = function (event) {
		console.log(event.data);
	};
console.timeEnd("start")
console.log("end");

}

function locfor()
{ 
console.log("start")
console.time("start")
var cout=0;
	for(var i=0;i<1000000000;i++)
	{
		cout++
	}
	console.timeEnd("start")
	console.log("end");
	console.log(cout);
}

function Promisefor()
{ 

	var p1 = new Promise(function (resolve, reject) {
		var cout=0;
		for(var i=0;i<1000000000;i++)
		{
			cout++
		}
		resolve(cout)
	});
	p1.then(function(res){
		console.log(res)
	})
	console.log("hello");
	
}

var w=null;

function startWorker()
{
if(typeof(Worker)!=="undefined")
  {
  if(w===null)
  {
  w=new Worker("demo_workers.js");
  }
  w.onmessage = function (event) {
    document.getElementById("result").innerHTML=event.data;
    };
  }
else
  {
  document.getElementById("result").innerHTML="Sorry, your browser does not support Web Workers...";
  }
}

function stopWorker()
{ 
w.terminate();
w=null;
}



</script>

